package com.yjian.controller;

import com.yjian.auth.AuthService;
import com.yjian.common.SDataResult;
import com.yjian.controller.bean.*;
import com.yjian.service.MemberService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Min;

/**
 * Created by 许小宝 on 2017/9/27.
 */
@RestController
@RequestMapping(value = "/api/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private AuthService authService;

    @ApiOperation(value = "获取店铺所有会员列表", notes = "获取店铺所有会员列表")
    @GetMapping("/v1")
    public Object getMemberListByUid(HttpServletRequest httpServletRequest) {
        try {
            String uid = authService.getUidFromRequestHeader(httpServletRequest);
            return memberService.getMemberListByUid(uid);
        } catch (Exception e) {
            outPutErrorLog(e);
        }
        return SDataResult.fail();
    }

    @ApiOperation(value = "店铺添加会员", notes = "店铺添加会员")
    @PostMapping("/v1")
    public Object saveMember(@Valid @RequestBody SaveMemberRequest request,
                             HttpServletRequest httpServletRequest) {

        try {
            String uid = authService.getUidFromRequestHeader(httpServletRequest);
            return memberService.saveMember(uid,request.getPhone(),request.getUsername(),request.getBalance(),request.getNumber());
        } catch (Exception e) {
            outPutErrorLog(e);
        }
        return SDataResult.fail();
    }

    @ApiOperation(value = "获取会员详情", notes = "获取会员详情")
    @PostMapping(value = "/v1/get")
    public Object getMemberByMid(@Valid @RequestBody GetMemberByMidRequest request,
                                 HttpServletRequest httpServletRequest) {
        try {
            String uid = authService.getUidFromRequestHeader(httpServletRequest);
            return memberService.getMemberByMid(uid, request.getMid());
        } catch (Exception e) {
            outPutErrorLog(e);
        }
        return SDataResult.fail();
    }

    @ApiOperation(value = "分页获取会员信息可以手机号搜索", notes = "分页获取会员信息可以手机号搜索")
    @PostMapping("/v1/{pageNo}/{pageSize}")
    public Object getMemberPageByUid(@Valid@PathVariable("pageNo")@Min(1) Integer pageNo,
                                     @Valid@PathVariable("pageSize")@Min(1) Integer pageSize,
                                     @Valid@RequestBody GetMemberPageByUidRequest request,
                                     HttpServletRequest httpServletRequest) {
        try {
            String uid = authService.getUidFromRequestHeader(httpServletRequest);
            return memberService.getMemberPageByUid(uid,request.getPhone(), pageNo, pageSize);
        } catch (Exception e) {
            outPutErrorLog(e);
        }
        return SDataResult.fail();
    }

    @ApiOperation(value = "会员消费金额", notes = "会员消费金额")
    @PutMapping("/v1/1/balance")
    public Object consumeMemberBalance(@Valid @RequestBody ConsumeMemberBalanceRequest request,
                                       HttpServletRequest httpServletRequest) {
        try {
            String uid = authService.getUidFromRequestHeader(httpServletRequest);
            return memberService.consumeMemberBalance(uid, request.getMid(), request.getBalance());
        } catch (Exception e) {
            outPutErrorLog(e);
        }
        return SDataResult.fail();
    }

    @ApiOperation(value = "会员消费次数", notes = "会员消费次数")
    @PutMapping("/v1/1/number")
    public Object consumeMemberNumber(@Valid @RequestBody ConsumeMemberNumberRequest request,
                                      HttpServletRequest httpServletRequest) {
        try {
            String uid = authService.getUidFromRequestHeader(httpServletRequest);
            return memberService.consumeMemberNumber(uid,request.getMid(),request.getNumber());
        } catch (Exception e) {
            outPutErrorLog(e);
        }
        return SDataResult.fail();
    }

    @ApiOperation(value = "会员充值金额", notes = "会员充值金额")
    @PutMapping("/v1/2/balance")
    public Object addMemberBalance(@Valid @RequestBody AddMemberBalanceRequest request,
                                   HttpServletRequest httpServletRequest) {
        try {
            String uid = authService.getUidFromRequestHeader(httpServletRequest);
            return memberService.addMemberBalance(uid, request.getMid(), request.getBalance());
        } catch (Exception e) {
            outPutErrorLog(e);
        }
        return SDataResult.fail();
    }

    @ApiOperation(value = "会员充值次数", notes = "会员充值次数")
    @PutMapping("/v1/2/number")
    public Object addMemberNumber(@Valid @RequestBody AddMemberNumberRequest request,
                                  HttpServletRequest httpServletRequest) {
        try {
            String uid = authService.getUidFromRequestHeader(httpServletRequest);
            return memberService.addMemberNumber(uid,request.getMid(), request.getNumber());
        } catch (Exception e) {
            outPutErrorLog(e);
        }
        return SDataResult.fail();
    }

    private void outPutErrorLog(Exception e) {
        System.out.println(e.getMessage());
    }
}
